package GUI;

import Domain.DomainController;
import Domain.Player.Human;
import Domain.Player.Player;
import java.awt.event.*;
import java.io.Serializable;
import javax.swing.JLabel;
import java.util.regex.*;
import javax.swing.JOptionPane;

/**
 * HumanListener handles a player's (Human type) mouse clicks, representing
 * their next move.
 */
public class HumanListener extends MouseAdapter implements Serializable{

    private DomainController domainController;

    public HumanListener(DomainController domainController) {
        this.domainController = domainController;
    }/*
     * 
     */


    /**
     * Client clicked the game board
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("pressed");
        Player player = domainController.getCurrentPlayer();

        // Ignore mouse click (not a Human):
        if (!(player instanceof Human)) {
            return;
        }

        Human humanPlayer = (Human) player;

        try {
            String name = ((JLabel) e.getSource()).getName();
            String[] temp = name.split("\\s+");
            int column = Integer.parseInt(temp[1]);
            
            int row = Integer.parseInt(temp[0]);
            humanPlayer.setNextMove(row, column);
        } catch (NumberFormatException ex) {
            System.out.println("Faulty column value passed to parseInt: " + ex);
        } catch (ClassCastException ex) {
            System.out.println("Typecast failed: " + ex);
        }/**/
    }
}
